/**
 **************************************************************************
 * @file     netconf.c
 * @version  v2.1.1
 * @date     2022-07-22
 * @brief    network connection configuration
 **************************************************************************
 *                       Copyright notice & Disclaimer
 *
 * The software Board Support Package (BSP) that is made available to
 * download from Artery official website is the copyrighted work of Artery.
 * Artery authorizes customers to use, copy, and distribute the BSP
 * software and its related documentation for the purpose of design and
 * development in conjunction with Artery microcontrollers. Use of the
 * software is governed by this copyright notice and the following disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
 * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
 * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
 * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
 * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 **************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "lwip/memp.h"
#include "lwip/tcp.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/udp.h"
#include "netif/etharp.h"
#include "lwip/dhcp.h"
#include "ethernetif.h"
#include "netconf.h"
#include "stdio.h"
#include "at32_emac.h"
#include "tcp_client.h"
#include "mbapp.h"

/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define MAC_ADDR_LENGTH (6)
#define ADDR_LENGTH (4)
#define SYSTEMTICK_PERIOD_MS 10
/* Private variables ---------------------------------------------------------*/
extern volatile uint32_t local_time;
extern struct netif netif;

static volatile uint32_t tcp_timer = 0;
static volatile uint32_t arp_timer = 0;
static volatile uint32_t link_timer = 0;
static volatile uint32_t tcp_send_timer = 0;

#if LWIP_DHCP
static volatile uint32_t dhcp_fine_timer = 0;
static volatile uint32_t dhcp_coarse_timer = 0;
#endif

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
 * @brief  lwip periodic tasks
 * @param  localtime the current localtime value
 * @retval none
 */
void tcp_mbclient_poll(void);

void lwip_periodic_client(volatile uint32_t localtime, char netst)
{
	if (!netst)
		return;

	if (localtime - tcp_send_timer >= 1000)
	{
		struct tcp_pcb *pcb_first, *pcb;

		tcp_send_timer = localtime;
		pcb = check_tcp_connect();

		if (pcb != NULL)
		{
			/*
			pcb_first = pcb;
			do
			{
				tcp_client_send_data(pcb, (unsigned char *)TCP_CLIENT_TEST_DATA, sizeof(TCP_CLIENT_TEST_DATA)); // Send data to TCP server actively
				pcb = pcb->next;
			} while ((pcb_first != pcb) && (pcb != NULL));
			
			if (tcp_client_obj.pcb01->connected)
				tcp_client_send_data(tcp_client_obj.pcb01, (unsigned char *)TCP_CLIENT_TEST_DATA, sizeof(TCP_CLIENT_TEST_DATA));
			if (tcp_client_obj.pcb02->connected)
				tcp_client_send_data(tcp_client_obj.pcb02, (unsigned char *)TCP_CLIENT_TEST_DATA, sizeof(TCP_CLIENT_TEST_DATA));
			if (tcp_client_obj.pcb03->connected)
				tcp_client_send_data(tcp_client_obj.pcb03, (unsigned char *)TCP_CLIENT_TEST_DATA, sizeof(TCP_CLIENT_TEST_DATA));
			*/
		}
	}

	tcp_mbclient_poll();
}
